package com.mt.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mt.enums.Status;
import com.mt.exception.GlobalException;
import com.mt.mapper.CinemaHallMapper;
import com.mt.mapper.CinemaMapper;
import com.mt.pojo.Cinema;
import com.mt.pojo.CinemaHall;
import com.mt.service.CinemaHallService;
import com.mt.service.CinemaSeatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Wang
 * @since 2020-12-11
 */
@Service
public class CinemaHallServiceImpl extends ServiceImpl<CinemaHallMapper, CinemaHall> implements CinemaHallService {
    @Resource
    CinemaHallMapper hallMapper;

    @Resource
    CinemaMapper cinemaMapper;

    @Autowired
    CinemaSeatService seatService;

    /**
     * 查询电影院的所有电影厅
     * */
    @Override
    public IPage<CinemaHall> pageHall(int currentPage, int pageSize, String hallName, int cinemaId) {
        Cinema cinema = cinemaMapper.selectById(cinemaId);
        IPage<CinemaHall> page = new Page<>(currentPage,pageSize);
        if (cinema != null){
            hallName = StrUtil.isBlank(hallName) ?null : "%"+hallName+"%";
            List<CinemaHall> halls = hallMapper.listHall(page,hallName,cinemaId);
            page.setRecords(halls);
            return page;
        }else {
            throw new GlobalException(Status.NOT_EXIST_CINEMA);
        }
    }

    @Override
    public List<CinemaHall> listAllHall(int cinemaId) {
        List<CinemaHall> halls = hallMapper.getAllHall(cinemaId);
        return halls;
    }

    @Override
    public CinemaHall getHallInfo(Integer hallId) {
        CinemaHall hall = hallMapper.selectById(hallId);
        return hall;
    }

    /**
     * 新建电影厅,并且默认分配座位
     * */
    @Override
    public boolean insertHall(CinemaHall hall) {
        CinemaHall cinemaHall = hallMapper.selectOne(Wrappers.<CinemaHall>lambdaQuery().eq(CinemaHall::getCinemaId,hall.getCinemaId())
                .eq(CinemaHall::getHallName, hall.getHallName()));
        if (cinemaHall != null){
            throw new GlobalException(Status.EXIST_CINEMA_HALL);
        }
        int addHall = hallMapper.insert(hall);
        if (addHall >0){
            boolean seat2Hall = seatService.defaultSeat2Hall(hall.getId(), hall.getMaxRow(), hall.getMaxColumn());
            return seat2Hall;
        }else {
            throw new GlobalException(Status.ERROR);
        }
    }


    /**
     * 批量新建电影厅
     * */
    @Override
    public boolean batchInsertHall(List<CinemaHall> hallList) {
        for (CinemaHall hall: hallList) {
            this.insertHall(hall);
        }
        return true;
    }


    /**
     * 修改电影厅信息
     * */
    @Override
    public boolean modifyHall(CinemaHall hall) {
        CinemaHall cinemaHall = hallMapper.selectOne(Wrappers.<CinemaHall>lambdaQuery().eq(CinemaHall::getHallName, hall.getHallName()));
        if (cinemaHall != null){
            throw new GlobalException(Status.EXIST_CINEMA_HALL);
        }
        int update = hallMapper.updateById(hall);
        if (update <=0){
            throw new GlobalException(Status.ERROR);
        }
        return true;
    }

    @Override
    public boolean deleteHall(CinemaHall hall) {
        seatService.deleteAllHallSeat(hall.getId());
        hallMapper.deleteById(hall.getId());
        return true;
    }
}
